package com.ruoyi.ocean.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ocean.domain.bo.OceanAccountBo;
import com.ruoyi.ocean.domain.vo.OceanAccountVo;
import com.ruoyi.ocean.domain.OceanAccount;
import com.ruoyi.ocean.mapper.OceanAccountMapper;
import com.ruoyi.ocean.service.IOceanAccountService;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 广告主账户Service业务层处理
 *
 * @author linziheng
 * @date 2024-05-09
 */
@RequiredArgsConstructor
@Service
public class OceanAccountServiceImpl implements IOceanAccountService {

    private final OceanAccountMapper baseMapper;

    /**
     * 查询广告主账户
     */
    @Override
    public OceanAccountVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }
    @Override
    public    int queryByAdvertiserId(Long advertiserId){
        return baseMapper.queryByAdvertiserId(advertiserId);
    }

    /**
     * 查询广告主账户列表
     */
    @Override
    public TableDataInfo<OceanAccountVo> queryPageList(OceanAccountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OceanAccount> lqw = buildQueryWrapper(bo);
        Page<OceanAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询广告主活跃账户列表
     */
    @Override
    public  List<OceanAccountVo> queryActiveList() {
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DAY_OF_MONTH, -31);
        Date previousDate = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String yesterdays = sdf.format(previousDate);
        return baseMapper.queryActiveList(yesterdays);
    }
    /**
     * 查询广告主活跃账户列表
     */
    @Override
    public void updateYes(Long agentId, Date yesterday){
        baseMapper.updateYes(agentId, yesterday);
    }
    /**
     * 查询广告主活跃账户列表
     */
    @Override
    public List<OceanAccountVo> queryListByPage(int i, int size,String type,String name){
        return baseMapper.queryListByPage(i, size,type,name);
    }
    /**
     * 查询广告主活跃账户列表
     */
    @Override
    public int count(String accountType,String name){
        return baseMapper.count(accountType,name);
    }
    /**
     * 查询广告主账户列表
     */
    @Override
    public List<OceanAccountVo> queryList(OceanAccountBo bo) {
        LambdaQueryWrapper<OceanAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OceanAccount> buildQueryWrapper(OceanAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OceanAccount> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAgentName()), OceanAccount::getAgentName, bo.getAgentName());
        lqw.like(StringUtils.isNotBlank(bo.getCusName()), OceanAccount::getCusName, bo.getCusName());
        lqw.eq(bo.getAdvertiserId() != null, OceanAccount::getAdvertiserId, bo.getAdvertiserId());
        lqw.eq(bo.getActiveDate() != null, OceanAccount::getActiveDate, bo.getActiveDate());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountType()), OceanAccount::getAccountType, bo.getAccountType());
        return lqw;
    }

    /**
     * 新增广告主账户
     */
    @Override
    public Boolean insertByBo(OceanAccountBo bo) {
        OceanAccount add = BeanUtil.toBean(bo, OceanAccount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改广告主账户
     */
    @Override
    public Boolean updateByBo(OceanAccountBo bo) {
        OceanAccount update = BeanUtil.toBean(bo, OceanAccount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OceanAccount entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除广告主账户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
